Inheritance

class Person
{
private string name; // 파상클래스에 상속 안됨
protected int age; // 파생클래스에 상속 됨

public Person() : this(“HCI”, 20)
{
}

 

public Person(string name, int age)
{
this.name = name;
this.age = age;
}

 

protected string GetName() // 파생클래스에 상속 안됨
{
return name;
}

 

protected void SetName(string name) // 파생클래스에 상속 안됨
{
this.name = name;
}

 

public void Print()
{
Console.WriteLine(“Person Name: {0} Age: {1}”, name, age);
}
}

 

class Student : Person
{
int id;
public Student() // public Student : base()와 같은 의미, 생성자는 상속안됨
{
id = 5208;
}
public Student(string name, int age, int id) : base(name, age)
{
this.id = id;
}
public void Print()
{
Console.WriteLine(“Student Name: {0} Age: {1} Id: {2}”, GetName(), age, id);
}
}

 

class Program
{
static void Main(string[] args)
{
Person p = new Person();
p.Print();
Person p1 = new Person(“HCI2~~~~~~~~~~”, 2013);
p1.Print();

Student s = new Student();
s.Print();
Student s1 = new Student(“학생들~~”, 24, 555);
s1.Print();
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *